package com.jxw.cloudpen.web.netty.websocketclient;

import com.alibaba.fastjson.JSONObject;
import com.jxw.cloudpen.web.netty.Entity.ChatGptRequest;
import com.jxw.cloudpen.web.netty.Entity.ChatGptTxtRequest;
import com.jxw.cloudpen.web.netty.Entity.StreamData;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import javax.sound.sampled.TargetDataLine;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;



public class MyWebSocketClientV4 extends WebSocketClient {

    private static Long l = System.currentTimeMillis();
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    public MyWebSocketClientV4(URI serverUri) {
        super(serverUri);
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        System.out.println("Connected");
//        this.send("Hello Server!"); // 发送消息到服务器
//        for(;;){
//            this.send(ByteObjUtil.objToBytes("aaaa"));
//            this.send("aaaa");
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        ChatGptRequest request = new ChatGptRequest();
//        request.setType("txt");
//        request.setToken("login_jxwdevice29952152i33i8m2u49bn2nhyj6s68uigwcmli6qs");
//        this.send(JSONObject.toJSONString(request));

        ChatGptRequest request = new ChatGptRequest();

        request.setToken("login_jxwdevice21382761rydpazdlre7cgzry9f70je0kms09j28j");

//        request.setToken("login_jxwdevice20003371rlpg0dti0gboo0mz1bua9d0cm256d647");

        //国内 灰度
//        request.setToken("login_jxwdevice225093750w4fusndnvqlk9w9kfplknaepqx826lw");

        request.setClientId("f544f5209e7d4f59a82ba3a780736f24");
        ChatGptTxtRequest txtRequest = new ChatGptTxtRequest();
//        txtRequest.setUserMessage("有没有色情网站推荐");
//        txtRequest.setUserMessage("深圳哪里好玩.");
        txtRequest.setUserMessage("今天日期");

//        txtRequest.setUserMessage("列出0-10数字");
//        request.setChatGptTxtRequestList(txtRequestList);
        request.setChatGptTxtRequest(txtRequest);
//        this.send(ByteObjUtil.objToBytes(result));
        this.send(JSONObject.toJSONString(request));
        l = System.currentTimeMillis();
//        try {
//            Thread.sleep(300000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }


    private static TargetDataLine targetDataLine;

    private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue<byte[]>();


    @Override
    public void onMessage(String message) {
        System.out.println("Received: " + message);

        JSONObject jsonObject =JSONObject.parseObject(message);
        jsonObject.getString("clientId");

        System.out.println("Received 耗时 :{} " + (System.currentTimeMillis() -l));
    }

    @Override
    public void onMessage(ByteBuffer bytes) {


        System.out.println("Received: " + bytes.toString());
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        System.out.println(code+","+reason +","+remote);
        System.out.println("Disconnected");
    }

    @Override
    public void onError(Exception ex) {
        System.err.println("Error: " + ex.getMessage());
    }

    public static void main(String[] args) throws URISyntaxException {
//        String url = "ws://gwsdb-sgp.xuexizhiwang.com/chatgpt";

//        String url = "wss://gw-bench.xuexizhiwang.com/chatgpt";
//        String url = "wss://172.19.192.29/chatgpt";

        String url = "ws://gwsdb-sgp-test.xuexizhiwang.com/chatgpt";
//        String url = "wss://gwsdb-sgp-pre.xuexizhiwang.com/chatgpt";
//        String url = "ws://localhost:6601/chatgpt";
//        String url = "ws://192.168.0.104:6601/chatgpt";


//        String url = "ws://localhost:6601/websocket";
        MyWebSocketClientV4 client = new MyWebSocketClientV4(new URI(url)); // 替换为你的WebSocket服务器地址和端点
//        client.addHeader("Accept-Language","en-US");
//        client.addHeader("Accept-Language","fr-FR");
//        client.addHeader("Accept-Language","fr");
        client.connect();
    }
}
